﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using MasterSheetManager.Utl;


namespace MasterSheetManager.Models
{
    public class Employee
    {
        public static int CurrentRoleLVL
        {
            get { return GetRoleLevel(CurrentUserName); }
        }

        public static int GetRoleLevel(string name)
        {
            var role = GetRole(name);
            if (role != null)
                return GetRole(name).Level;
            else
                return -1;
        }

        public static Role GetRole(string name)
        {
            var roles = from r in CH.DB.Roles
                        where r.Name == name
                        select r;
            return roles.FirstOrDefault();

        }

        public static string CurrentUserName
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["CurrentUserName"] == null)
                {
                    HttpContext.Current.Items["CurrentUserName"] = GetCurrentUserName().ToLower();
                }
                if (HttpContext.Current == null)
                {
                    return "";
                }
                return HttpContext.Current.Items["CurrentUserName"] as string;
            }
            set
            {
                if (HttpContext.Current != null)
                    HttpContext.Current.Items["CurrentUserName"] = value;
            }
        }



        static string GetCurrentUserName()
        {
            var username = GetLoginUserName();

            if (LoginUserIsAdmin())
            {
                // Access system with simulator authority.
                SimulatorConfig simulatorConfig =
                    CH.DB.SimulatorConfig.FirstOrDefault(s => s.AdminName == username);
                if (simulatorConfig == null)
                {
                    return username;
                }
                return simulatorConfig.SimulatorName;
            }
            else
                return username;
        }



        /// <summary>
        /// Gets the name of current login user.
        /// </summary>
        /// <returns>User name of current logon user.</returns>
        public static string GetLoginUserName()
        {
            return HttpContext.Current.User.Identity.Name;
        }

        /// <summary>
        /// Gets the role of current login user.
        /// Note: This method only be used when admin login system with other role.
        /// In such case, Logon user is different from current user.
        /// </summary>
        /// <returns>User name of current logon user.</returns>
        public static bool LoginUserIsAdmin()
        {
            return GetRoleLevel(GetLoginUserName()) == LogonRequired.AdministratorRequired.LVL;
        }


        public static string GetRoleName(string name)
        {
            var role = GetRole(name);
            if (role != null)
                return role.Name;
            else
                return "未配置";
        }


        public static EmployeeRole GetPerson(string name)
        {
            var user = CH.DB.EmployeeRoles.Where(s => s.AccountName == name).FirstOrDefault();
            return user;
        }

        /// <summary>
        /// 通过Outlook取得
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static string GetContact(string Name)
        {
            return "";
        }

        internal static object GetAvailbleMembers(int projectId)
        {
            return from c in CH.DB.Members where projectId == c.ProjectID && c.IsActivated select c.Name;
        }
    }

    public class EmployeeRole : EntityBase
    {
        public virtual Role Role { get; set; }
        public int? RoleID { get; set; }

        [Display(Name = "英文名")]
        public string AccountName { get; set; }

        [Display(Name = "部门")]
        public int? DepartmentID { get; set; }
        public virtual Department Department { get; set; }

        [Display(Name = "工作职级")]
        public int? ExpLevelID { get; set; }
        public virtual ExpLevel ExpLevel { get; set; }

        [Display(Name = "入职时间")]
        public DateTime? StartDate { get; set; }

        [DataType(DataType.EmailAddress)]
        [Display(Name = "邮件地址")]
        public string Email { get; set; }

        [Display(Name = "中文名"), Required]
        public string AccountNameCN { get; set; }

        [Display(Name = "座机")]
        public int? AgentNum { get; set; }

        [Display(Name = "是否实习")]
        public bool? IsTrainee { get; set; }

        [Display(Name = "是否激活")]
        public bool IsActivated { get; set; }

        [DataType(DataType.PhoneNumber)]
        [Display(Name = "移动电话")]
        public string Mobile { get; set; }

        [Display(Name = "生日")]
        public DateTime? BirthDay { get; set; }

        [Display(Name = "员工性别")]
        public string Gender { get; set; }

        [Display(Name = "邮件签名")]
        public string EmailSignatures { get; set; }

        [Display(Name = "邮箱密码")]
        public string EmailPassword { get; set; }


    }

    /// <summary>
    /// 职级
    /// </summary>
    public class Role : EntityBase
    {
        public const int LVL_SuperManager = 800;
        public const int LVL_Director = 1000;
        public const int LVL_Manager = 500;
        public const int LVL_TeamLeader = 100;
        public const int LVL_Sales = 10;
        public const int LVL_ImportingInterface = 6;
        public const int LVL_ProductInterface = 5;
        public const int LVL_FinancialInterface = 4;
        public const int LVL_ConferenceInterface = 3;
        public const int LVL_PoliticsInterface = 2;
        public const int LVL_MarketInterface = 1;


        [Display(Name = "职级名称"), Required]
        public String Name { get; set; }

        [Display(Name = "职级分数"), Required]
        public int Level { get; set; }
    }

    /// <summary>
    /// 部门管理
    /// </summary>
    public class Department : EntityBase
    {
        [Display(Name = "部门名称"), Required]
        public String Name { get; set; }
    }

    public class ExpLevel : EntityBase
    {
        [Display(Name = "级别"), Required]
        public String Name { get; set; }

        [Display(Name = "部门")]
        public virtual Department Department { get; set; }

        [Display(Name = "部门")]
        public int? DepartmentID { get; set; }
    }

    public class SimulatorConfig : EntityBase
    {
        public string AdminName { get; set; }

        [Display(Name = "模拟人"), Required]
        public string SimulatorName { get; set; }
    }

    public class Member : EntityBase
    {
        [Display(Name = "名称")]
        public string Name { get; set; }
        public virtual Role Role { get; set; }
        public int? RolePMID { get; set; }
       
        [Display(Name = "项目")]
        public virtual int? ProjectID { get; set; }
        [Display(Name = "是否激活")]
        public virtual bool IsActivated { get; set; }
    }

    public class SR
    {

        public static string GobackToList { get { return "回到列表"; } }
        public static string Save { get { return "保存"; } }
        public static string Delete { get { return "删除"; } }
        public static string Details { get { return "详细"; } }
        public static string Edit { get { return "编辑"; } }
        public static string Create { get { return "创建"; } }
        public static string Required { get { return "此栏位不能为空"; } }
        public static string Form { get { return "表单"; } }
        public static string CannotDelete { get { return "此项已经被引用，无法直接删除，需要取消引用后再进行删除"; } }
        public static string CannotDownload { get { return "此文件不存在，不能继续下载"; } }
        public static string ErrorView { get { return @"~\views\shared\Error.cshtml"; } }
        public static string Confirm { get { return "确认"; } }

    }
}